/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.node.INode;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.system.entity.DictItem;
import org.springblade.modules.system.service.IDictItemService;
import org.springblade.modules.system.vo.DictItemVO;
import org.springblade.modules.system.wrapper.DictItemWrapper;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 字典表 控制器
 *
 * @author Blade
 * @since 2022-07-04
 */
@RestController
@AllArgsConstructor
@RequestMapping("/blade-system/dictitem")
@ApiIgnore
@Api(value = "字典", tags = "字典")
public class DictItemController extends BladeController {

	private IDictItemService dictItemService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入dictItem")
	public R<DictItem> detail(DictItem dictItem) {
		DictItem detail = dictItemService.getOne(Condition.getQueryWrapper(dictItem));
		return R.data(detail);
	}

	/**
	 * 分页 字典表
	 */
	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "type", value = "字典类型", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表", notes = "传入dictItem")
	public R<List<INode>> list(@ApiIgnore @RequestParam Map<String, Object> dict) {
		@SuppressWarnings("unchecked")
		List<DictItem> list = dictItemService.list(Condition.getQueryWrapper(dict, DictItem.class).lambda().orderByAsc(DictItem::getSort));
		return R.data(DictItemWrapper.build().listNodeVO(list));
	}

	@GetMapping("/getDictByType/{type}")
	public R<List<INode>> getDictByType(@PathVariable("type") String type) {
		DictItem dict = new DictItem();
		dict.setType(type);
		List<DictItem> list = dictItemService.list(Condition.getQueryWrapper(dict).lambda().orderByAsc(DictItem::getSort));
		return R.data(DictItemWrapper.build().listNodeVO(list));
	}

	/**
	 * 获取字典树形结构
	 *
	 * @return
	 */
	@GetMapping("/tree")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	public R<List<DictItemVO>> getTree(@ApiParam(value = "字典类型", required = false, defaultValue = "") @RequestParam String type) {
		List<DictItemVO> tree;
		if (StringUtil.isNotBlank(type)) {
			tree = dictItemService.selectTree(type);
		} else {
			tree = dictItemService.tree();
		}
		return R.data(tree);
	}

	/**
	 * 自定义分页 字典表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "分页", notes = "传入dictItem")
	public R<IPage<DictItemVO>> page(DictItemVO dictItem, Query query) {
		IPage<DictItemVO> pages = dictItemService.selectDictItemPage(Condition.getPage(query), dictItem);
		return R.data(pages);
	}

	/**
	 * 新增 字典表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "新增", notes = "传入dictItem")
	public R save(@Valid @RequestBody DictItem dictItem) {
		return R.status(dictItemService.save(dictItem));
	}

	/**
	 * 修改 字典表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "修改", notes = "传入dictItem")
	public R update(@Valid @RequestBody DictItem dictItem) {
		return R.status(dictItemService.updateById(dictItem));
	}

	/**
	 * 新增或修改 字典表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "新增或修改", notes = "传入dictItem")
	public R submit(@Valid @RequestBody DictItem dictItem) {
		return R.status(dictItemService.submit(dictItem));
	}

	/**
	 * 删除 字典表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(dictItemService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 获取字典
	 *
	 * @return
	 */
	@GetMapping("/dictionary")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "获取字典", notes = "获取字典")
	public R<List<DictItem>> dictionary(String type) {
		List<DictItem> tree = dictItemService.getList(type);
		return R.data(tree);
	}
}
